﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
   public class CostRegisterDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CostRegister model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CostRegisters(");
            strSql.Append("BaseItemNo,Driver,OrgzNo,CarNo,ProjectName,CostTime)");
            strSql.Append(" values (");
            strSql.Append("@BaseItemNo,@Driver,@OrgzNo,@CarNo,@ProjectName,@CostTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@BaseItemNo", SqlDbType.Int,4),
					new SqlParameter("@Driver", SqlDbType.Int,4),
					new SqlParameter("@OrgzNo", SqlDbType.Int,4),
					new SqlParameter("@CarNo", SqlDbType.Int,4),
					new SqlParameter("@ProjectName", SqlDbType.NVarChar,50),
					new SqlParameter("@CostTime", SqlDbType.DateTime)};
            parameters[0].Value = model.BaseItemNo;
            parameters[1].Value = model.Driver;
            parameters[2].Value = model.OrgzNo;
            parameters[3].Value = model.CarNo;
            parameters[4].Value = model.ProjectName;
            parameters[5].Value = model.CostTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CostRegister model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CostRegisters set ");
            strSql.Append("BaseItemNo=@BaseItemNo,");
            strSql.Append("Driver=@Driver,");
            strSql.Append("OrgzNo=@OrgzNo,");
            strSql.Append("CarNo=@CarNo,");
            strSql.Append("ProjectName=@ProjectName,");
            strSql.Append("CostTime=@CostTime");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@BaseItemNo", SqlDbType.Int,4),
					new SqlParameter("@Driver", SqlDbType.Int,4),
					new SqlParameter("@OrgzNo", SqlDbType.Int,4),
					new SqlParameter("@CarNo", SqlDbType.Int,4),
					new SqlParameter("@ProjectName", SqlDbType.NVarChar,50),
					new SqlParameter("@CostTime", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.BaseItemNo;
            parameters[1].Value = model.Driver;
            parameters[2].Value = model.OrgzNo;
            parameters[3].Value = model.CarNo;
            parameters[4].Value = model.ProjectName;
            parameters[5].Value = model.CostTime;
            parameters[6].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CostRegister GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from Vw_CostReg ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            CostRegister model = new CostRegister();
            DataTable ds = DbHelperSQL.Query(CommandType.Text, strSql.ToString(), parameters);
            if (ds.Rows.Count > 0)
            {
                return DataRowToModel(ds.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CostRegister DataRowToModel(DataRow row)
        {
            CostRegister model = new CostRegister();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["BaseItemNo"] != null && row["BaseItemNo"].ToString() != "")
                {
                    model.BaseItemNo = int.Parse(row["BaseItemNo"].ToString());
                    model.BaseItemName = row["BaseItemName"].ToString();
                }
                if (row["Driver"] != null && row["Driver"].ToString() != "")
                {
                    model.Driver = int.Parse(row["Driver"].ToString());
                    model.DriverName = row["DriverName"].ToString();
                }
                if (row["OrgzNo"] != null && row["OrgzNo"].ToString() != "")
                {
                    model.OrgzNo = int.Parse(row["OrgzNo"].ToString());
                    model.OrgzName = row["OrgzName"].ToString();
                }
                if (row["CarNo"] != null && row["CarNo"].ToString() != "")
                {
                    model.CarNo = int.Parse(row["CarNo"].ToString());
                    model.CarNoName = row["CarNoName"].ToString();
                }
                if (row["ProjectName"] != null)
                {
                    model.ProjectName = row["ProjectName"].ToString();
                }
                if (row["CostTime"] != null && row["CostTime"].ToString() != "")
                {
                    model.CostTime = DateTime.Parse(row["CostTime"].ToString());
                }
            }
            return model;
        }

        #endregion  BasicMethod
    }
}
